﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Collections.Specialized;
using HealthCenter.ciic;
using HealthCenter.MecreportProxyService;

namespace HealthCenter
{
    public partial class LoginForm : Form
    {
        //private string checkCodeUrl = "http://www.scars.org.cn/FPWEB/Login/CheckCode.aspx";
        //private string checkCodeUrl = "http://10.140.75.151:8001/ws/ScarsLogin.aspx";
        //private string checkCodeUrl = "http://localhost:2531/CheckCode.aspx";

        public LoginForm()
        {
            InitializeComponent();
            //RefreshCheckCode();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            //RXD_NationalSoapClient client = new RXD_NationalSoapClient();

            //loginSucceed = client.HandlerLoginScars(txtUserName.Text, txtPassword.Text, txtCheckCode.Text, GlobalInfo.cookie);
            //string key = "ASP.NET_SessionId";
            //string value = GlobalInfo.cookie.Substring(key.Length + 1, GlobalInfo.cookie.IndexOf(";") - key.Length);
            //Cookie c = new Cookie(key, value);
            //Console.WriteLine(GlobalInfo.cookie);
            //Console.WriteLine(value);
            //if (loginSucceed)
            //{
            //    MessageBox.Show("登录成功");
            //    this.Hide();
            //    ReportEditorForm f = new ReportEditorForm();
            //    f.Show();
            //}
            //else
            //{
            //    MessageBox.Show("登录失败，请检查用户名、密码、校验码是否正确！");
            //}

            MecReportProxyServiceSoapClient client = new MecReportProxyServiceSoapClient("MecReportProxyServiceSoap", GlobalInfo.ProxyServiceUrl);
            bool succeed = client.Login(txtUserName.Text, txtPassword.Text,txtCheckCode.Text, GlobalInfo.cookie);
            if (succeed)
            {
                this.Hide();
                ReportEditorForm f = new ReportEditorForm();
                f.Show();
            }
            else
            {
                MessageBox.Show("登录失败，请检查用户名、密码、校验码是否正确！");
            }


            //WebHeaderCollection c = GlobalInfo.Client.Headers;
            //c.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
            //c.Add(HttpRequestHeader.Referer, "http://www.scars.org.cn/FPWEB/Login/Login.aspx");
            //c.Add(HttpRequestHeader.AcceptLanguage, "zh-cn");
            //c.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; InfoPath.1; .NET CLR 2.0.50727; CIBA; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)");
            //c.Add(HttpRequestHeader.Pragma, "no-cache");
            //c.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
            //c.Add(HttpRequestHeader.Accept, "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*");
            //c.Add(HttpRequestHeader.Cookie, GlobalInfo.cookie);
            //c.Add(HttpRequestHeader.KeepAlive, "true");

            //string userName = txtUserName.Text;
            //string password = txtPassword.Text;
            //string checkCode = txtCheckCode.Text;
            //string url = "http://www.scars.org.cn/FPWEB/Login/Login.aspx";
            //NameValueCollection form = new NameValueCollection();
            //form.Add("Users", userName);
            //form.Add("Users_Password", password);
            //form.Add("CheckCode", checkCode);
            //form.Add("__EVENTVALIDATION", "/wEWBgKF7p7JBwLl//HgAgKf8fmIBQLO44u1DQL+jNCfDwK3tNX0DlA1ZeklWSti5XcJCI/Oz8MkdmpP");
            //form.Add("__EVENTTARGET", "");
            //form.Add("__EVENTARGUMENT", "");
            //form.Add("__VIEWSTATE", "/wEPDwULLTEwMTA2MDUyNTNkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYCBQtMb2dpbkJ1dHRvbgUIUmVnaXN0ZXL3eoW4qMe2sP6Lg0ZDpdIKtCRBfg==");
            //form.Add("LoginButton.x", "38");
            //form.Add("LoginButton.y", "14");

            //byte[] response = GlobalInfo.Client.UploadValues(url, "POST", form);

            //string ret = Encoding.Default.GetString(response);


            //Console.WriteLine("\n\n\n\n2");
            //Console.WriteLine(GlobalInfo.Client.ResponseHeaders["Set-Cookie"]);
            //Console.WriteLine("\n\n\n\n");

            //Console.WriteLine(ret);

            //if (ret.Contains(@"/wEPDwUKLTUxMTcwNzgxMGRkQ7Cmk+F0ZwDbJzNyYnAWjk7m/Bg="))
            //{
            //    loginSucceed = true;
            //    this.Hide();
                //byte[] bs = GlobalInfo.client.DownloadData("http://www.scars.org.cn/FPWEB/Report/ReportEdit/RP_ADR_Edit.aspx");
                //string s = Encoding.GetEncoding("GB2312").GetString(bs);
                //Console.WriteLine("$$$$$$$$$$$$$$$$");
                //Console.WriteLine(s);
                //ReportEditorForm f = new ReportEditorForm();
                //f.Show();

            //}
            //else
            //{
            //    MessageBox.Show("登录失败，请检查用户名、密码、校验码是否正确！");
            //}
            //byte[] bys = GlobalInfo.Client.DownloadData("http://www.scars.org.cn/FPWEB/MyDeskTop/UsersInfo.aspx");
            //string s = Encoding.Default.GetString(bys);
            //Console.WriteLine(s);
        }

        private void RefreshCheckCode()
        {
            try
            {
                byte[] bs = GlobalInfo.Client.DownloadData(GlobalInfo.CheckCodeUrl);
                MemoryStream stream = new MemoryStream(bs);
                GlobalInfo.cookie = GlobalInfo.Client.ResponseHeaders["Set-Cookie"];
                Console.WriteLine("\n\n\n1");
                Console.WriteLine(GlobalInfo.cookie);
                Console.WriteLine("\n\n\n");

                Image image = Image.FromStream(stream);
                pbCheckCode.Image = image;
            }
            catch (WebException)
            {
                MessageBox.Show("同服务器通信出现故障，请检查网络连接！");
            }
        }

        private void pbCheckCode_Click(object sender, EventArgs e)
        {
            RefreshCheckCode();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            SystemConfigForm form = new SystemConfigForm();
            form.ShowDialog();
        }

        private void LoginForm_Shown(object sender, EventArgs e)
        {
            RefreshCheckCode();
        }
        private bool loginSucceed = false;
        public bool LoginSucceed
        {
            get
            {
                return loginSucceed;
            }
        }
    }
}
